package com.chen.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.blog.common.api.CommonResult;
import com.chen.blog.common.constant.MessageConstant;
import com.chen.blog.mapper.BlogMapper;
import com.chen.blog.model.Blog;
import com.chen.blog.model.Type;
import com.chen.blog.mapper.TypeMapper;
import com.chen.blog.service.TypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author chen
 * @since 2022-01-24
 */
@Service
@Transactional
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements TypeService {

    @Autowired
    private TypeMapper typeMapper;

    @Autowired
    private BlogMapper blogMapper;

    /**
     * 添加分类功能实现
     *
     * @param type
     * @return
     */
    @Override
    public CommonResult add(Type type) {
        Type realType = typeMapper.selectOne(new QueryWrapper<Type>().eq("type_name", type.getTypeName()));
        if (realType != null) {
            return CommonResult.error(MessageConstant.TYPE_NAME_FAIL);
        }
        if (typeMapper.insert(type) > 0) {
            return CommonResult.success(MessageConstant.ADD_POSITION_SUCCESS);
        }
        return CommonResult.error(MessageConstant.ADD_POSITION_FAIL);
    }

    /**
     * 批量删除分类功能实现
     *
     * @param ids
     * @return
     */
    @Override
    public CommonResult deleteByIds(Integer[] ids) {
        for (Integer id : ids) {
            List<Blog> blogList = blogMapper.selectList(new QueryWrapper<Blog>().eq("type_id", id));
            if (blogList != null && blogList.size() > 0) {
                for (Blog blog : blogList) {
                    blog.setTypeId(0);
                    blogMapper.updateById(blog);
                }
            }
        }
        if (typeMapper.deleteBatchIds(Arrays.asList(ids)) > 0) {
            return CommonResult.success(MessageConstant.DELETE_POSITION_SUCCESS);
        }
        return CommonResult.error(MessageConstant.DELETE_POSITION_FAIL);
    }

    /**
     * 更新分类功能实现
     *
     * @param type
     * @return
     */
    @Override
    public CommonResult updateName(Type type) {
        Type realType = typeMapper.selectOne(new QueryWrapper<Type>().eq("type_name", type.getTypeName()));
        if (realType != null) {
            return CommonResult.error(MessageConstant.TYPE_NAME_FAIL);
        }
        type.setUpdateTime(LocalDateTime.now());
        if (typeMapper.updateById(type) > 0) {
            return CommonResult.success(MessageConstant.UPDATE_POSITION_SUCCESS);
        }
        return CommonResult.error(MessageConstant.UPDATE_POSITION_FAIL);
    }

    /**
     * 检索不同分类下的博客数量功能实现
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> findBlogCountByType() {
        return typeMapper.findBlogCountByType();
    }

    /**
     * 检索指定分类下的博客功能
     *
     * @param id
     * @return
     */
    @Override
    public List<Map<String, Object>> findBlogByTypeId(Integer id) {
        return typeMapper.findBlogByTypeId(id);
    }

    /**
     * 分页复合查询类型功能实现
     *
     * @param currentPage
     * @param pageSize
     * @param type
     * @return
     */
    @Override
    public CommonResult getByParamAndPage(Integer currentPage, Integer pageSize, Type type) {
        Page<Type> page = typeMapper.selectPage(new Page<>(currentPage, pageSize), new QueryWrapper<Type>()
                .like(type.getId() != null, "id", type.getId())
                .like(!StringUtils.isEmpty(type.getTypeName()), "type_name", type.getTypeName()));
        if (currentPage > page.getPages()) {
            return CommonResult.success(null, typeMapper.selectPage(new Page<>(page.getPages(), pageSize), new QueryWrapper<Type>().
                    like(type.getId() != null, "id", type.getId()).
                    like(!StringUtils.isEmpty(type.getTypeName()), "type_name", type.getTypeName())));
        }
        return CommonResult.success(null, page);
    }

}
